import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import '../platform_interface/permission_platform_interface.dart';

/// An implementation of [PermissionPlatform] that uses method channels.
class MethodChannelPermission extends PermissionPlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('permission');

  late final StreamController<bool> _adapterStateController;
  late final Stream<bool> _adapterState;

  MethodChannelPermission(){
    _adapterStateController = StreamController<bool>.broadcast();
    _adapterState = _adapterStateController.stream;

    methodChannel.setMethodCallHandler(_onMethodCall);
  }


  @override
  Stream<bool> get adapterState => _adapterState;

  Future<void> _onMethodCall(MethodCall call) async{
    switch(call.method){
      case "onAdapterStateChanged":
        bool result = call.arguments;
        _adapterStateController.add(result);
        break;
      default:
        break;
    }
  }

  @override
  Future<String?> getPlatformVersion() async {
    final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
    return version;
  }
}
